package models

import (
	_ "github.com/go-sql-driver/mysql"
	"github.com/astaxie/beego/orm"
	"regexp"
	"strings"
	// "fmt"
)

var (
	db orm.Ormer
)

type MovieInfo struct {
	Id int64
  Movie_name string
	Movie_id int64
  Movie_pic string
  Movie_director string
  Movie_writer string
  Movie_country string
	Movie_language string
  Movie_main_character string
  Movie_type string
  Movie_on_time string
  Movie_span string
  Movie_grade string
}

func init() {
	orm.Debug = true // 是否开启调式模式 调式模式下回打印出sql语句
	orm.RegisterDataBase("default", "mysql", "root:hjp7468668578@tcp(127.0.0.1:3306)/gomysql?charset=utf8", 30)
	orm.RegisterModel(new(MovieInfo))
	db = orm.NewOrm()
}

// 插入数据库
func AddMovie(movie_info *MovieInfo) (int64, error) {
	id, err := db.Insert(movie_info)
	return id, err
}

// 获取导演 Movie_director
func GetMovieDirector(movieHtml string) string{
	if movieHtml == "" {
		return ""
	}
	reg := regexp.MustCompile(`<a.*?rel="v:directedBy">(.*?)</a>`)
	result := reg.FindAllStringSubmatch(movieHtml, -1)
	if len(result) == 0 {
		return ""
	}
	return string(result[0][1])
}

// 获取电影名字 Movie_name
func GetMovieName(movieHtml string) string{
	if movieHtml == "" {
		return ""
	}
	reg := regexp.MustCompile(`<span\s*property="v:itemreviewed">(.*?)</span>`)
	result := reg.FindAllStringSubmatch(movieHtml, -1)
	if len(result) == 0 {
		return ""
	}
	return string(result[0][1])
}

// 获取主演 Movie_main_character
func GetMovieMainCharacters(movieHtml string) string{
	reg := regexp.MustCompile(`<a.*?rel="v:starring">(.*?)</a>`)
	result := reg.FindAllStringSubmatch(movieHtml, -1)
	if len(result) == 0 {
		return ""
	}
	mainCharacters := ""
	for _, v := range result{
		mainCharacters += v[1] + "/"
	}
	return strings.Trim(mainCharacters, "/")
}

// 获取图片 Movie_pic
func GetMoviePic(movieHtml string) string{
	reg := regexp.MustCompile(`<img.*?rel="v:image" />`)
	result := reg.FindAllStringSubmatch(movieHtml, -1)
	if len(result) == 0 {
		return ""
	}
	return string(result[0][0])
}

// 获取评分 movie_grade
func GetMovieGrade(movieHtml string) string {
	reg := regexp.MustCompile(`<strong.*?property="v:average">(.*?)</strong>`)
	result := reg.FindAllStringSubmatch(movieHtml, -1)
	if len(result) == 0 {
		return ""
	}
	return string(result[0][1])
}

// 获取类型 movie_type
func GetMovieGenre(movieHtml string) string {
	reg := regexp.MustCompile(`<span.*?property="v:genre">(.*?)</span>`)
	result := reg.FindAllStringSubmatch(movieHtml, -1)
	if len(result) == 0 {
		return ""
	}
	movieGenre := ""
	for _, v := range result {
		movieGenre += v[1] + "/"
	}
	return strings.Trim(movieGenre, "/")
}

// 获取时长  movie_span
func GetMovieTime(movieHtml string) string {
	reg := regexp.MustCompile(`<span.*?property="v:runtime".*?>(.*?)</span>`)
	result := reg.FindAllStringSubmatch(movieHtml, -1)
	if len(result) == 0 {
		return ""
	}
	return string(result[0][1])
}

// 获取上映时间 Movie_on_time
func GetMovieOnTime(movieHtml string) string {
	reg := regexp.MustCompile(`<span.*?property="v:initialReleaseDate".*?>(.*?)</span>`)
	result := reg.FindAllStringSubmatch(movieHtml, -1)
	if len(result) == 0 {
		return ""
	}
	ti := result[0][1]
	num := strings.Index(ti, "(")
	return string(ti[0:num])
}

// 获取 电影url链接
func GetMovieUrls(movieHtml string) []string {
	reg := regexp.MustCompile(`<a.*?href="(https://movie.douban.com/.*?)"`)
	result := reg.FindAllStringSubmatch(movieHtml, -1)

	var movieSets []string
	for _,v := range result {
		movieSets = append(movieSets, v[1])
	}
	return movieSets
}

